package cn.fl.esign.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.esign.service.ICfcaUsrService;
import cn.fl.esign.service.IFilSignInfoService;
import cn.fl.esign.vo.FilSignInfoVO;
import cn.fl.esign.voTransform.FilSignInfoVOTransform;
import cn.fl.framework.base.context.CurrentThreadContext;
import tc.esign.facade.esign.api.ICfcaUsrApiService;
import tc.esign.facade.esign.api.IFilSignInfoApiService;
import tc.esign.facade.esign.condition.CfcaUsrQueryCondition;
import tc.esign.facade.esign.constant.FilSignConstant;
import tc.esign.facade.esign.dto.CfcaUsrDTO;
import tc.esign.facade.esign.dto.FilSignInfoDTO;

/**
 * @Description: 合同签署明细表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class FilSignInfoServiceImpl implements IFilSignInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IFilSignInfoService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	//@Autowired
	//private I自定义ApiService 自定义ApiService;
	@Autowired
	private IFilSignInfoApiService filSignInfoApiService;
	
	@Autowired
	private ICfcaUsrApiService cfcaUsrApiService;
	
	@Autowired
	private ICfcaUsrService cfcaUsrService;

	@Override
	public FilSignInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		FilSignInfoDTO dto = null;//this.自定义ApiService.selectById(id);
		return FilSignInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(FilSignInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.filSignInfoApiService.add(FilSignInfoVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.filSignInfoApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(FilSignInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.filSignInfoApiService.modify(FilSignInfoVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.filSignInfoApiService.searchListPage(page);
		List<FilSignInfoVO> list = this.setCfcaUsrData((List<FilSignInfoDTO>)page.getResultList());
		page.setResultList(list);
		return page;
	}
	public List<FilSignInfoVO> setCfcaUsrData(List<FilSignInfoDTO> list) throws BaseException{
		List<FilSignInfoVO> listVO = new ArrayList<FilSignInfoVO>();
		if(CollectionUtils.isNotEmpty(list)){
			List<String> listUserIds = new ArrayList<String>();
			CfcaUsrQueryCondition cfcaUsrQc = new CfcaUsrQueryCondition();
			for(FilSignInfoDTO dto: list){
				listUserIds.add(dto.getUserId());
			}
			cfcaUsrQc.setUserIds(listUserIds);
			List<CfcaUsrDTO> listCfcaUsrDto = this.cfcaUsrApiService.getListByQc(cfcaUsrQc);
			Map<String,String> nameMap = new HashMap<String,String>();
			Map<String,String> phoneMap = new HashMap<String,String>();
			for(CfcaUsrDTO dto : listCfcaUsrDto){
				nameMap.put(dto.getUserId(), dto.getUserNm());
				phoneMap.put(dto.getUserId(), dto.getMobilePhone());
			}
			for(FilSignInfoDTO dto: list){
				FilSignInfoVO vo = new FilSignInfoVO();
				vo = FilSignInfoVOTransform.toVO(dto);
				vo.setSignName(nameMap.get(vo.getUserId()));
				vo.setSignNm(phoneMap.get(vo.getUserId()));
				if(StringUtils.isBlank(vo.getSignName())){
					vo.setSignName(dto.getSignNm());
				}
				if(StringUtils.isBlank(vo.getSignNm())){
					vo.setSignNm(dto.getPhoneNumber());
				}
				if(String.valueOf(FilSignConstant.CODE_SIGN_INFO_STS_1).equals(vo.getSignSts()+"")){
					 vo.setSignStsName("已签署");
				 }else{
					 vo.setSignStsName("未签署");
				 }
				listVO.add(vo);
			}
		}
		return listVO;
	}
}
